package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 套餐的分页查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        //创建分页查询对象
        Page<Setmeal> page = new Page<>(pageNum,pageSize);
        //创建查询条件对象
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        //设置条件
        wrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        page = setmealMapper.selectPage(page, wrapper);

        //获取结果集
        List<Setmeal> setmealList = page.getRecords();
            for (Setmeal setmeal : setmealList) {
                //查询套餐类
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                //将菜品分类名称赋值给套餐
                setmeal.setCategoryName(category.getName());

                //创建套餐菜品查询条件
                LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
                //设置条件
                wrapper1.eq(SetmealDish::getSetmealId,setmeal.getId());
                //查询套餐菜品
                List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper1);
                setmeal.setSetmealDishes(setmealDishList);
            }
        return page;
    }

    /**
     * 添加套餐
     * @param setmeal
     */
    @Override
    public void save(Setmeal setmeal) {
        long id = IdWorker.getId();
        setmeal.setId(id);
        setmeal.setCode(String.valueOf(id));
        setmeal.setStatus(1);
        setmealMapper.insert(setmeal);

        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //将套餐的id设置给套餐菜品
            setmealDish.setSetmealId(setmeal.getId());
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        }
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        LambdaQueryWrapper<SetmealDish> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);
        setmeal.setSetmealDishes(setmealDishes);
        return setmeal;
    }

    /**
     * 修改套餐
     * @param setmeal
     */
    @Override
    public void update(Setmeal setmeal) {
        setmealMapper.updateById(setmeal);
        LambdaQueryWrapper<SetmealDish>  wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishMapper.delete(wrapper);

        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishes)){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    /**
     * 批量停售或起售
     * @param status
     * @param ids
     */
    @Override
    public void updateStatus(Integer status, Long[] ids) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(ids!=null && ids.length>0,Setmeal::getId,ids);
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmealMapper.update(setmeal,wrapper);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public String deleteById(Long[] ids) {

        String messAge= "";
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selectById(id);
            //判断销售状态
            if (setmeal.getStatus() == 0){
                LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(SetmealDish::getSetmealId,id);
                setmealDishMapper.delete(wrapper);
                setmealMapper.deleteById(id);
            }else {
                messAge+=setmeal.getName()+",";
            }
        }
        return messAge;
    }

    @Override
    public List<Setmeal> findAll(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(categoryId!=null,Setmeal::getCategoryId,categoryId);
        wrapper.eq(Setmeal::getStatus,status);
        List<Setmeal> setmeals = setmealMapper.selectList(wrapper);
        return setmeals;
    }

    /**
     * 根据套餐id查询菜品
     * @param id
     * @return
     */
    @Override
    public List<SetmealDish> findBySetmealId(Long id) {
        List<SetmealDish> setmealDishes = setmealDishMapper.findBySetmealId(id);
        return setmealDishes;
    }


}
